﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data;


namespace DAL
{
    public class ProjectDAL
    {
        private UserInfo user = new UserInfo();
        /// <summary>
        ///增加一行项目
        /// </summary>
        /// <param name="project">项目实体类</param>
        /// <returns></returns>
        public bool AddProject(ProjectInfo project,UserInfo user)
        {
            try
            {
                if (DBHelper.ExecuteSQL($"insert ProjectInfo(UserID,ProjectType,ProjectName,Project_state,Label) values('{user.UserID}','{project.ProjectType}','{project.Project_Name}','{project.Project_state}','{project.Label}')") < 0)
                {
                    return false;
                }

                return true;
            }
            catch (Exception)
            {

                return false;
            }

        }



        /// <summary>
        /// 执行项目查询，返回ds
        /// </summary>
        /// <returns></returns>
        public DataSet GetProjectList()
        {
            try
            {
                string sql = $"select * from ProjectInfo where 1=1";

                sql += " order by ProjectID";

                return DBHelper.ExucuteSQL_DataSet(sql);
            }
            catch (Exception)
            {
                return null;
            }

        }

        /// <summary>
        /// 执行项目查询，返回ds
        /// </summary>
        /// <returns></returns>
        public DataSet GetProjectList(int i)
        {
            try
            {
                return DBHelper.ExucuteSQL_DataSet($"select * from ProjectInfo where UserID={i}");
            }
            catch (Exception)
            {
                return null;
            }
            
        }

        /// <summary>
        /// 执行项目查询，返回ds
        /// </summary>
        /// <param name="project">传入项目数据</param>
        /// <returns></returns>
        public DataSet GetProjectList(ProjectInfo project)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from ProjectInfo where 1=1");
                //判断项目状态
                if (project.Project_state.Length > 0)
                {
                    sb.Append($" and Project_state='{project.Project_state}'");
                }
                //判断项目id
                if (project.ProjectID != 0)
                {
                    sb.Append($" and ProjectID='{project.ProjectID}'");
                }
                //判断项目类型
                if (project.ProjectType.Length > 0)
                {
                    sb.Append($" and ProjectType='{project.ProjectType}'");
                }
                //判断用户id
                if (user.UserID != 0)
                {
                    sb.Append($" and UserID='{user.UserID}'");
                }
                return DBHelper.ExucuteSQL_DataSet(sb.ToString());
            }
            catch (Exception)
            {

                return null;
            }

        }

        /// <summary>
        /// 项目管理，改变项目状态
        /// </summary>
        /// <param name="user">传入项目实体类，主要id</param>
        /// <returns></returns>
        public bool AdminProject(ProjectInfo project)
        {
            try
            {
                if (DBHelper.ExecuteSQL($"update ProjectInfo set " +
$"project.Project_state={project.Project_state} " +
$"where ProjectID='{project.ProjectID}' ") < 1)
                {
                    return false;
                }
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
    }
}
